package ideamaps.action;


import ideamaps.domain.User;
import ideamaps.manager.UserManager;

import com.taillive.commons.TextUtils;
import com.taillive.core.web.StrutsAction;

public class RegisterAction extends StrutsAction{
	
	private User user;
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	private String confirmPassword;
	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	
	private UserManager userManager;
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public void validateRegister(){
		if(isGet())
			return ;
		
		if(TextUtils.isEmpty(user.getUsername())){
			addActionError("Username is required");
		}
		
		if(TextUtils.isEmpty(user.getEmail())){
			addActionError("Email is required");
		}
		
		if(TextUtils.isEmpty(user.getPassword())){
			addActionError("Password is required");
		}
		
		if(confirmPassword==null || !confirmPassword.equals(user.getPassword())){
			addActionError("Confirm password is invalid");
		}
		
		
		User eUser = userManager.getByUsername(user.getUsername());
		if(eUser!=null)
			addActionError("Username has exist");
		
	}

	public String register(){
		if(isGet()){
			return INPUT;
		}else{
			
			userManager.newRegister(user);
			getSession().setAttribute("sessionUsername",user.getUsername());
			return SUCCESS;
		}
	}



}
